//
//  OPMembersList.m
//  OPFramework
//
//  Created by malcom on 7/24/08.
//  Copyright 2008 malcom-mac software. All rights reserved.
//

#import "OPMembersList.h"
#import "OPUser.h"
#import "OPBoard.h"

#define _method_getMembersList				@"members.getMembersList"
#define _method_getMembersListWithAdvSearch @"members.getMembersListWithAdvancedSearch"

@implementation OPMembersList

@synthesize p_itemsPerPage;
@synthesize p_paginatedList;

- (id) initToListUsersStartsWithChar:(char) _char fromBoard:(OPBoard *) _board {
	self = [super initWithBoard: _board];
	if (self != nil) {
		p_itemsPerPage = 10;
		p_paginatedList = [[OPPageContainer alloc] init];
		
		// parameters list
		_fctParameters = [[NSMutableArray alloc] initWithObjects: [NSNumber numberWithInt: 0],[NSNumber numberWithInt: p_itemsPerPage],[NSString stringWithFormat: @"%c",_char],nil];
		_calledMethod = [_method_getMembersList retain];
	}
	return self;
}

- (id) initToSearchUsersWithCriteria:(NSDictionary *) _searchCriteria fromBoard:(OPBoard *) _board {
	self = [super initWithBoard: _board];
	if (self != nil) {
		p_itemsPerPage = 10;
		p_paginatedList = [[OPPageContainer alloc] init];
		
		// parameters list
		_fctParameters = [[NSMutableArray alloc] initWithObjects:	@"searchuser",
																	[NSNumber numberWithInt: 0],
																	[NSNumber numberWithInt: p_itemsPerPage],
																	@"",
																	@"a",
																	@"a",
																	_searchCriteria,nil];
		_calledMethod = [_method_getMembersListWithAdvSearch retain];
	}
	return self;
}
	
- (id) initToSearchCommonPropertiesContains:(NSString *) _containsString fromBoard:(OPBoard *) _board {
	NSDictionary *criteria = [[NSDictionary alloc] initWithObjectsAndKeys:	_containsString,@"username",
																				_containsString,@"email",
																				_containsString,@"aim",
																				_containsString,@"yahoo",
																				_containsString,@"msn",
																				_containsString,@"jabber",nil,nil];
	return [self initToSearchUsersWithCriteria: [criteria autorelease] fromBoard:_board];
}

- (NSArray *) getMembersPage:(NSNumber *) _pgIdx {
	if (p_paginatedList.p_totalPages == 0 || [self isCacheExpired]) {
		
		int idxOfPageIndex;
		int idxOfItemsPerPage;
		
		// standard members list
		if ([_calledMethod isEqual: _method_getMembersList]) {
			idxOfPageIndex = 0;
			idxOfItemsPerPage = 1;
		}
		
		// advanced search
		if ([_calledMethod isEqual: _method_getMembersListWithAdvSearch]) {
			idxOfPageIndex = 1;
			idxOfItemsPerPage = 2;
		}
		
		// replace variable configuration parameters
		[_fctParameters replaceObjectAtIndex: idxOfPageIndex withObject: _pgIdx];
		[_fctParameters replaceObjectAtIndex: idxOfItemsPerPage withObject: [NSNumber numberWithInt: p_itemsPerPage]];

		[self sendXMLRequestForMethod: _calledMethod withParams: _fctParameters parseSelector: @selector(_parseResultsList:)];
	}
	return [p_paginatedList getPageAtIndex: _pgIdx];
}

- (void)connection: (XMLRPCConnection *)connection didReceiveResponse:(XMLRPCResponse *)response forMethod: (NSString *)method {
	// super class check if it's an error, a numeric response or simply some data to parse
	if (![self _checkErrorInRawResponseFromServer: response forMethod:method]) {
		// data update to parse
		[self _parseResultsList: [NSArray arrayWithObjects: [response responseObject],method,nil]];
		// we can propagate the result to listeners
		[self dataUpdatedFor: self atMethod:method];	
	}
}

- (BOOL) _isNumericResponseFromServerAnErrorCode:(NSNumber *) _code forMethod:(NSString *) method {
	switch ([_code intValue]) {
		case OFAPI_MEMBERSLIST_ANONYMOUS_NOT_ALLOWED:
		case OFAPI_MEMBERSLIST_NOT_AUTHORIZED:
			return YES;
			break;
	}
	return NO;
}

- (void) _parseResultsList:(NSArray *) _result {
	NSDictionary *res = [_result objectAtIndex:0];
	
	p_paginatedList.p_totalPages = [[res objectForKey:@"total_pages"] intValue];
	p_paginatedList.p_totalElements = [[res objectForKey: @"total_users"] intValue];
	
	NSMutableArray *page = [[NSMutableArray alloc] init];
	for (NSDictionary *cUser in [res objectForKey: @"users_list"]) {
		OPUser *user = [_delegateBoard.obj_membersCache getUserMemberFromID: [NSNumber numberWithInt: [[cUser objectForKey: @"user_id"] intValue]] 
						withName: [cUser objectForKey: @"username"]];
		[page addObject: user];
	}
	[p_paginatedList addPage: page atIndex: [res objectForKey: @"current_page"]];

}

@end
